﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Model
{
    public class DeleteService
    {
        public ServiceResult<bool> DeletePromotionalActivity(Guid id)
        {
            using (JzSport.Model.JzSportDbContext dbContext = new JzSportDbContext())
            {
                EntityRepository<PromotionalActivity> reps = new EntityRepository<PromotionalActivity>(dbContext);

                PromotionalActivity pActivity = reps.Get(p => p.Id == id);

                if (pActivity != null)
                {
                    reps.Delete(pActivity);

                    if (pActivity.ActivityId != Guid.Empty)
                    {
                        EntityRepository<Activity> aRep = new EntityRepository<Activity>(dbContext);
                        Activity activity = aRep.Get(p => p.Id == pActivity.ActivityId);
                        if (activity != null)
                        {
                            aRep.Delete(activity);
                        }
                    }

                    try
                    {
                        dbContext.SaveChanges();
                        return new ServiceResult<bool>(ErrorCode.NoError);
                    }
                    catch (Exception ex)
                    {
                        return new ServiceResult<bool>(ErrorCode.DB_Error, false, ex);
                    }
                }
                else
                {
                    return new ServiceResult<bool>(ErrorCode.NoError);
                }
            }
        }
    }
}
